package indoor.nexd.com.nexdmap.nexdmapoverly;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import cn.nexd.map.rendering.core.SVGMapBaseOverlay;
import cn.nexd.map.rendering.core.componet.SVGMapView;
import indoor.nexd.com.nexdmap.R;

/**
 * 地图左上角指南针图层
 * <p/>
 * Created by wangxu on 16/5/19.
 */
public class CompassOverlay extends SVGMapBaseOverlay {
    private ImageView view;
    private float compassRotate = 0f;
    private SVGMapView nexdMap;
    private Context context;
    private Bitmap bitmap;

    public CompassOverlay(Context context, View view, SVGMapView nexdMap) {
        this.context = context;
        this.view = (ImageView) view;
        this.nexdMap = nexdMap;
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onTap(MotionEvent motionEvent) {

    }


    @Override
    public void draw(Canvas canvas, Matrix matrix, float v, float v1) {
        canvas.save();
        if (bitmap == null) {
            bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.zhinanzen);
            view.setImageBitmap(bitmap);
        }
        RotateAnimation ra = new RotateAnimation(nexdMap.getCurrentRotateDegrees(), -compassRotate,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        // 设置动画的持续时间
        ra.setDuration(200);
        // 设置动画结束后的保留状态
        ra.setFillAfter(true);
        // 启动动画
        view.startAnimation(ra);
        compassRotate = -nexdMap.getCurrentRotateDegrees();
        canvas.restore();
    }
}
